<?php
// +----------------------------------------------------------------------
// | 工程进度管理
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class ConstructionController extends AdminBaseController
{
    public function initialize()
    {
        $this->cate_model = Db::name("Cate");
        $this->deal_model = Db::name("Goods");
        $this->room_model = Db::name("room");
        $this->log_model = Db::name("outinLog");
        parent::initialize();
    }

    public function index()
    {
        $pro_name = $this->request->param("pro_name");
        $results = Db::name('project')->alias("p")->field("p.*,a.approval_status")
            ->leftJoin("approval a ","a.form_id = p.id and a.form_type='3' and a.detail_type='2' ")
            ->where('p.pro_name', 'like', '%' . $pro_name . '%')
            ->order('id',"desc")
            ->paginate(15);
//        echo Db::name('project')->getLastSql();

        $page = $results->render();
        $this->assign("results", $results);
        $this->assign("pro_name", $results);
        $this->assign("page", $page);
        return $this->fetch();
    }
    //物料申购
    public function index_add() {

        Db::name('material_purchase')->insert();
        $this->success("添加成功！", url("Construction/index"));
    }

    public function add()
{
    return $this->fetch();
}

    //物料弹出列表
    public function applylist() {
        $rooms=$this->room_model->where(['if_show'=>1,'is_delete'=>0])->select();
        $this->assign('rooms',$rooms);

        $type = $this->request->param("type",0);
        $this->assign("type",empty($type) ? '' : $type);

        $where = ['d.type'=>1,'d.is_delete'=>0,'d.if_show'=>1];
        //商品名称
        $data = $this->request->param('goods_name','');
        //库房名
        $room = $this->request->param("room",0);
        $this->assign('room',$room);
        empty($room)?$where:($where=['d.type'=>$type,'d.is_delete'=>0,'d.if_show'=>1,'r.id'=>$room]);
        //取库房数据
        $results = $this->deal_model
            ->alias("d")
            ->field("d.*,c.cate_name,r.room_name")
            ->join("dj_cate c","d.cate_p_id = c.id","left")
            ->join("dj_room r","r.id = d.room","left")
            ->where('d.goods_name','like','%'.$data.'%')->where($where)->order('d.id', 'DESC')->paginate(12);
        $this->assign("results",$results);
        // 页面传值
        $page = $results->render();
        $this->assign("page", $page);
        return $this->fetch();
    }
    /**
     * @param int $type
     * @param $pro_id
     */
    public function ajaxproduct(){
        $keyword = $this->request->param('keyword','');
        $where = ['d.type'=>1,'d.is_delete'=>0,'d.if_show'=>1];
        $results = $this->deal_model
            ->alias("d")
            ->field("d.*,d.id as goods_id,r.room_name")
            ->join("dj_room r","r.id = d.room","left")
            ->where('d.goods_name', 'like', '%' . $keyword . '%')
            ->where($where)
            ->order("d.listorder","ASC")
            ->paginate(10);
        $this->assign("keyword", $keyword);
        $this->assign("result", $results);
        $this->assign("page", $results->render());
        return $this->fetch('goods_list');

    }
    public function applyadd_post()
    {
        if ($this->request->isPost()){
            $param = $this->request->param();
            $project_id = $this->request->param("porject_id");

            if (!isset( $param['product'])){
                $this->error('请选择产品');
            }

            $product = $param['product'];

            // 保存dj_project_materiel主表
            $metarial = array();
            $metarial['project_id'] =$project_id;
            $metarial['add_time'] =time();
            $metarial['apply_user'] =cmf_get_current_admin_id();
            $metarialid = Db::name("project_materiel")->insertGetId($metarial);

            if($metarialid != null){
                // 循环保存dj_project_materiel_goods
                $metarialgoods = array();
                foreach ($product as $pk=>&$value){
                    $metarialgoods['materiel_id'] =$metarialid;
                    $metarialgoods['goods_id'] =$value['id'];
                    $metarialgoods['num'] =$value['num'];
                    $metarialgoods['tax_money'] =$value['tax_money'];
                    $metarialgoods['total_price'] =$value['total_price'];

                    Db::name("project_materiel_goods")->insert($metarialgoods);
                }

                // 先保存dj_approval审批表
                $appdata = array();
                $appdata['approval_status'] = '1';
                $appdata['approval_desc'] = '';
                $appdata['form_type'] = '1';
                $appdata['detail_type'] = '1';
                $appdata['apply_time'] = time();
                $appdata['apply_user'] = cmf_get_current_admin_id();
                $appdata['form_id'] = $metarialid;

                Db::name("approval")->insert($appdata);
            }

            if ($metarialid != null){
                $this->success('提交成功',url('index'));
            }else{
                $this->error('提交失败');
            }
        }
        $users = Db::name("user")->alias('u')->field('u.id,user_nickname,c.department_name')
            ->join('dj_department c','c.id=u.dept_id')
            ->where(['u.user_type'=>2,'c.dept_type'=>5])
            ->select();
        $this->assign("userlist", $users);

        $result['pact_num'] = PurchaseModel::contractSn();
        $result['add_time'] = date('Y-m-d H:i');
        $this->assign("results", $result);
        $this->assign("page", '');
        return $this->fetch();
    }
    /*发起项目*/
    /**
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function add_post()
    {
        $data = $this->request->param();

        $plan_time = $data['plan_time'];
        $completion_time = $data['completion_time'];
        $data['plan_time'] = strtotime($plan_time);
        $data['completion_time'] = strtotime($completion_time);
        $data['creat_time'] = time();
        $data['add_user'] = cmf_get_current_admin_id();
        $id = Db::name('project')->insertGetId($data);

        // 插入审核表
        $appdata = array();
        $appdata['approval_status'] = '1';
        $appdata['approval_desc'] = '';
        $appdata['form_type'] = '3';
        $appdata['detail_type'] = '2';
        $appdata['apply_time'] = time();
        $appdata['apply_user'] = cmf_get_current_admin_id();
        $appdata['form_id'] = $id;

        Db::name("approval")->insert($appdata);

        $this->success("添加成功！", url("Construction/index"));
    }
    /*查看审批中项目*/
    public function getdetail()
    {
        $id = $this->request->param('id');

        $result = Db::name("project")->alias("a")
            ->field('a.*,u.user_nickname')
            ->leftJoin("user u", "u.id=a.add_user")
            ->leftJoin("approval ap", "ap.form_id=a.id")
            ->leftJoin("user u1","ap.approval_user=u1.id")
            ->where('a.id', $id)
            ->find();

        if($result){

            $result['completion_time'] = date('Y/m/d',$result['completion_time']);
            $result['plan_time'] = date('Y/m/d',$result['plan_time']);
            $result['creat_time'] = date('Y/m/d H:i:s',$result['creat_time']);

            $a = Db::name("approval")->alias("ap")
                ->field("ap.approval_status,ap.approval_desc,u1.user_nickname approval_user,ap.approval_time")
                ->leftJoin("user u1","ap.approval_user=u1.id")
                ->where('ap.form_id', $result['id'])
                ->where("ap.form_type","3")
                ->where("ap.detail_type","2")
                ->find();

            $result['approval_time']  = date('Y/m/d H:i:s',$a['approval_time']);
            $result['approval_status']  = $a['approval_status']?$a['approval_status']:"未提交";
            $result['approval_user']  =$a['approval_user'];
            $result['approval_desc']  =$a['approval_desc'];
        }
        $this->assign($result);
        $this->assign("show_flag", "1");

        return $this->fetch();
    }

    /*查看申购历史记录*/
    public function getmateriellist()
    {
        $id = $this->request->param('id');

        $resultlist = Db::name("project_materiel")->alias("am")
            ->field('pg.id as am_id,pg.*,g.goods_name,g.stock,g.price,g.price_unit,g.goods_num,r.room_name,
            ap.approval_status,ap.approval_desc,u1.user_nickname approval_user,ap.approval_time')
            ->leftJoin("project_materiel_goods pg", "pg.materiel_id=am.id")
            ->leftJoin("goods g","g.id=pg.goods_id and g.type='1'")
            ->leftJoin("room r","r.id=g.room")
            ->leftJoin("approval ap", "ap.form_id=am.id")
            ->leftJoin("user u1","ap.approval_user=u1.id")
            ->where('am.project_id', $id)
            ->where("ap.form_type","1")
            ->where("ap.detail_type","1")
            ->select();

        $this->assign("resultslist", $resultlist);

        $this->assign("show_flag", "1");

        return $this->fetch();
    }

    /*物料申购*/
    public function apply()
    {
        // 物料申购页面
        $id = $this->request->param("id");
        $this->assign("porject_id",$id);

        return $this->fetch();
    }

    /*工程进度页面跳转*/
    public function schedule()
    {
        $id = $this->request->param("id");
        $result = Db::name("project")
            ->where('id',$id)
            ->find();

        $results = Db::name('project_schedule')->alias('ps')
            ->field('ps.progress_content,ps.add_time,p.plan_time,p.completion_time,p.construction_peo,p.creat_time')
            ->join('project p','p.id=ps.project_id')
            ->where('ps.project_id',$id)->select();

        //echo Db::name("project_schedule")->getLastSql();
        $this->assign($result);
        $this->assign("results",$results);
        return $this->fetch();
    }

    /*工程进度提交*/
    public function schedule_post()
    {
        $data = $this->request->param();
        $project_id = $this->request->param("project_id");
        $data['project_id'] = $project_id;
        $data['add_time'] = time();

        Db::name('project_schedule')->insert($data);
        $this->success("添加成功！", url("Construction/index"));
    }

    /*验收竣工 页面跳转*/
    public function accepting()
    {
        $id = $this->request->param("id");
        $result = Db::name("project")
            ->where('id',$id)
            ->find();
        $this->assign($result);
        return $this->fetch();
    }

    /*验收竣工  提交*/
    public function accepting_post()
    {
        $project_id = $this->request->param("project_id");
        $data['project_id'] = $project_id;
        $data = $this->request->param();
        Db::name('project_accepting')->insert($data);
        $this->success("添加成功！", url("Construction/index"));
    }

    /*验收维护 页面跳转*/
    public function weihu()
    {
        $id = $this->request->param("id");
        $result = Db::name("project")
            ->where('id',$id)
            ->find();
        $this->assign($result);
        return $this->fetch();
    }

    /*验收维护 提交*/
    public function weihu_post()
    {
        $project_id = $this->request->param("project_id");
        $data['project_id'] = $project_id;
        $data = $this->request->param();
        $start_time = $data['start_time'];
        $end_time = $data['end_time'];
        $data['start_time'] = strtotime($start_time);
        $data['end_time'] = strtotime($end_time);
        Db::name('project_weihu')->insert($data);
        $this->success("添加成功！", url("Construction/index"));
    }

}